//package com.lyt.springboot.Interceptor;
//
//import com.lyt.springboot.Exception.UnPermissionException;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.concurrent.TimeUnit;
//
//@Slf4j
//@Component
//public class PermissionInterceptor implements HandlerInterceptor {
//    @Autowired
//    private StringRedisTemplate redisTemplate;
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//
////        // 获取Token
////        String bearerToken = request.getHeader("Authorization");
////        if (StringUtils.isEmpty(bearerToken)) {
////            throw new UnPermissionException();
////        }
////        // Bearer
////        if (!bearerToken.startsWith("Bearer")) {
////            throw new UnPermissionException();
////        }
////        String[] tokens = bearerToken.split(" ");
////        if (tokens.length != 2) {
////            throw new UnPermissionException();
////        }
////        String jwtToken = tokens[1];
////        return true;
//
//        //获取访问 ip 地址
////        String ipAddr = getIpAddr(request);
//        // 存入缓存10s不允许访问
//        String key = new StringBuilder().append("Ky").toString();
//
//        if (redisTemplate.hasKey(key)) {
//            // 访问次数自增1
//            redisTemplate.opsForValue().increment(key, 1);
//            String value = redisTemplate.opsForValue().get(key);
//            if (Integer.valueOf(value) > 3) {
//                log.warn("访问过快，存在强刷行为！key={}", key);
//                return false;
//            }
//        } else {
//            // 第一次访问
//            redisTemplate.opsForValue().set(key, "1", 100,
//                    TimeUnit.SECONDS);
//        }
//        return true;
//
//    }
//
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//
//    }
//
//    public static String getIpAddr(HttpServletRequest request) {
//        String ipAddress = null;
//        try {
//            ipAddress = request.getHeader("X-Forwarded-For");
//            if (ipAddress != null && ipAddress.length() != 0 && !"unknown".equalsIgnoreCase(ipAddress)) {
//                // 多次反向代理后会有多个ip值，第一个ip才是真实ip
//                if (ipAddress.indexOf(",") != -1) {
//                    ipAddress = ipAddress.split(",")[0];
//                }
//            }
//            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
//                ipAddress = request.getHeader("Proxy-Client-IP");
//            }
//            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
//                ipAddress = request.getHeader("WL-Proxy-Client-IP");
//            }
//            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
//                ipAddress = request.getHeader("HTTP_CLIENT_IP");
//            }
//            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
//                ipAddress = request.getRemoteAddr();
//            }
//        } catch (Exception e) {
//
//        }
//        return ipAddress;
//    }
//}
